//列表
loadMyEssay();
var currentPage = 1;

function loadMyEssay() {
	$(document).ready(function() {
		init(currentPage);
	});

	function init(pageNo) {
		$("#goodsCarList").html("");
		$.ajax({
			url: urlcore + "/api/goodsCar/selectGoodsCarList2?current=" + pageNo,
			type: "get",
			dataType: 'json',
			contentType: "application/json;charset=utf-8",
			success: function(data) {
				if(data.success == true) {
					var saleNums = 0; //所有商品个数
					$.each(data.data, function(i, n) {
						
						if(n.goodsList!=null){
							//商品
						var goodsList = '';
						$.each(n.goodsList, function(i, m) {
							//单价商品购买个数
							var saleNum = 0;
							var goodsStockList = ''; //库存
							$.each(m.myStockList, function(i, y) {
								saleNum += y.saleNum;
								goodsStockList +=
									'<tr>' +
									'<td>' +
									'<span>' + y.size.sizeName + '</span>' +
									'</td>' +
									'<td>' + y.colour.colourName + '</td>' +
									'<td>' + y.saleNum + '件</td>' +
									'<td class="saleNum" hidden="hidden"><input type="hidden" class="saleNum1" value="' + y.saleNum + '"/></td>' +
									'</tr>';

							});

							saleNums += saleNum;
							//全部商品
							$("#allSale").text("全部商品(" + saleNums + ")");
							//单件商品金额
							var money = m.tradePrice * saleNum;
							var shixiao = '';
							if(m.status == 2) {
								shixiao = '(失效)';
							}

							goodsList +=
								'<div class="buy_goods_input">' +
								'<input type="checkbox" name="checkedId" value="' + m.id + '"  class="check goods-check goodsCheck">' +
								'</div>' +
								'<div class="buy_goods_right">' +
								'<div class="clearfix detail_goods">' +
								'<dl class="fl clearfix money1">' +
								'<dt>' +
								'<a href="2.2.0goods_detail.html?id=' + m.id + '"><img src="' + m.coverUrl + '" /></a>' +
								'</dt>' +
								'<dd class="money2">' +
								'<a href="2.2.0goods_detail.html?id=' + m.id + '"><h5>' + m.goodsTitle + '' + shixiao + '</h5></a>' +
								'<h6>&yen;' + m.tradePrice + '</h6>' +
								'<h6 class="money3" hidden="hidden"><input type="hidden" class="money4" value="' + m.tradePrice + '"/></h6>' +
								'</dd>' +
								'</dl>' +
								'<div class="fl detail_goods_table">' +
								'<table>' +
								goodsStockList +
								'</table>' +
								'</div>' +
								'<div class="fl detail_goods_price">' + fmoney(money, 2) + '</div>' +
								'<div class="buy_goods_caozuo">' +
								'<span class="public-Number" onclick="goodsStockDetailById(' + m.id + ');">修改数量</span>' +
								'<span class="Shoucang" id="Shoucang" onclick="coll(' + m.id + ')">收藏</span>' +
								'<span onclick="del(' + m.id + ')">删除</span>' +
								'</div>' +
								'</div>' +
								'</div>';

						});

						var thislist =
							'<div class="buy_goods">' +
							'<div class="buy_goods_title clearfix">' +
							'<div class="buy_title_input">' +
							'<input type="checkbox" class="check goods-check shopCheck">' +
							'</div>' +
							'<a href="10.1.0dianpu_detail.html?id=' + n.id + '"><h5>' + n.storeName + '</h5></a>' +
							'</div>' +
							'<div class="buy_goods_xinxi clearfix">' +
							goodsList +
							'</div>' +
							'</div>';
						$('#goodsCarList').append(thislist);
						}
						

					});
					//控制复选框
					$(function() {

						//全选
						$(".goodsCheck").click(function() {
							var goods = $(this).closest(".buy_goods").find(".goodsCheck"); //获取本店铺的所有商品
							var goodsC = $(this).closest(".buy_goods").find(".goodsCheck:checked"); //获取本店铺所有被选中的商品
							var Shops = $(this).closest(".buy_goods").find(".shopCheck"); //获取本店铺的全选按钮
							if(goods.length == goodsC.length) { //如果选中的商品等于所有商品
								Shops.prop('checked', true); //店铺全选按钮被选中
								if($(".shopCheck").length == $(".shopCheck:checked").length) { //如果店铺被选中的数量等于所有店铺的数量
									$("#AllCheck").prop('checked', true); //全选按钮被选中
								} else {
									$("#AllCheck").prop('checked', false); //else全选按钮不被选中 
								}
							} else { //如果选中的商品不等于所有商品
								Shops.prop('checked', false); //店铺全选按钮不被选中
								$("#AllCheck").prop('checked', false); //全选按钮也不被选中
							}
							var n1 = 0; //计算总个数
							var paymoney1 = 0; //计算总金额
							$(this).closest(".buy_goods").parents("#goodsCarList").find(".buy_goods").find(".goodsCheck").each(function(i, val) {
								if($(val).prop("checked") == true) {
									var n = 0;
									var t = $(val).parent().next().children().find('div[class*=detail_goods_table]').children().children().find('td[class*=saleNum]').find('input[class*=saleNum1]');
									if(t.length > 0) {
										$.each(t, function(i, v) {
											n += parseInt(v.value);
										});
									}
									var m = $(val).parent().next().children().find('dl[class*=money1]').find('dd[class*=money2]').find('h6[class*=money3]').find('input[class*=money4]');

									var paymoney = m.val() * n;
									paymoney1 += paymoney;
									n1 += n;
									$(".gooodsNum").text(n1);
									$(".paymoney").text(paymoney1);
									$(".paymoney1").text(fmoney(paymoney1, 2));
								} else {
									//反选递减
									var n = 0;
									var t = $(val).parent().next().find('div[class*=dingdan_goods]').find('div[class*=detail_goods_table]').children().children().find('td[class*=saleNum]').find('input[class*=saleNum1]');
									if(t.length > 0) {
										$.each(t, function(i, v) {
											n += parseInt(v.value);
										});
									}
									var m = $(val).parent().next().children().find('dl[class*=money1]').find('dd[class*=money2]').find('h6[class*=money3]').find('input[class*=money4]');

									var paymoney = m.val() * n;
									var gooodsNum = $(".gooodsNum").text() - n;
									var p = $(".paymoney").text() - paymoney;

									$(".gooodsNum").text(gooodsNum);
									$(".paymoney").text(p);
								}

							})
							//全不选中清空合计
							if($(this).closest(".buy_goods").parents("#goodsCarList").find(".buy_goods").find(".goodsCheck:checked").length == 0) {
								$(".gooodsNum").text('');
								$(".paymoney").text('');
								$(".paymoney1").text('');
							}
						});
						// 点击店铺按钮
						$(".shopCheck").click(function() {
							if($(this).prop("checked") == true) { //如果店铺按钮被选中
								$(this).parents(".buy_goods").find(".goods-check").prop('checked', true); //店铺内的所有商品按钮也被选中
								if($(".shopCheck").length == $(".shopCheck:checked").length) { //如果店铺被选中的数量等于所有店铺的数量
									$("#AllCheck").prop('checked', true); //全选按钮被选中
								} else {
									$("#AllCheck").prop('checked', false); //else全选按钮不被选中
								}
							} else { //如果店铺按钮不被选中
								$(this).parents(".buy_goods").find(".goods-check").prop('checked', false); //店铺内的所有商品也不被全选
								$("#AllCheck").prop('checked', false); //全选按钮也不被选中
							}

							var n1 = 0; //计算总个数
							var paymoney1 = 0; //计算总金额
							$(this).closest(".buy_goods").parents("#goodsCarList").find(".buy_goods").find(".goodsCheck").each(function(i, val) {

								if($(val).prop("checked") == true) {
									var n = 0;
									var t = $(val).parent().next().children().find('div[class*=detail_goods_table]').children().children().find('td[class*=saleNum]').find('input[class*=saleNum1]');
									if(t.length > 0) {
										$.each(t, function(i, v) {
											n += parseInt(v.value);
										});
									}
									var m = $(val).parent().next().children().find('dl[class*=money1]').find('dd[class*=money2]').find('h6[class*=money3]').find('input[class*=money4]');

									var paymoney = m.val() * n;
									paymoney1 += paymoney;
									n1 += n;
									$(".gooodsNum").text(n1);
									$(".paymoney").text(paymoney1);
									$(".paymoney1").text(fmoney(paymoney1, 2));
								} else {
									//反选递减
									var n = 0;
									var t = $(val).parent().next().find('div[class*=dingdan_goods]').find('div[class*=detail_goods_table]').children().children().find('td[class*=saleNum]').find('input[class*=saleNum1]');
									if(t.length > 0) {
										$.each(t, function(i, v) {
											n += parseInt(v.value);
										});
									}
									var m = $(val).parent().next().children().find('dl[class*=money1]').find('dd[class*=money2]').find('h6[class*=money3]').find('input[class*=money4]');

									var paymoney = m.val() * n;
									var gooodsNum = $(".gooodsNum").text() - n;
									var p = $(".paymoney").text() - paymoney;

									$(".gooodsNum").text(gooodsNum);
									$(".paymoney").text(p);
								}

							})
							//全不选中清空合计
							if($(this).closest(".buy_goods").parents("#goodsCarList").find(".buy_goods").find(".goodsCheck:checked").length == 0) {
								$(".gooodsNum").text('');
								$(".paymoney").text('');
								$(".paymoney1").text('');
							}
						});
						// 点击全选按钮
						$("#AllCheck").click(function() {
							if($(this).prop("checked") == true) { //如果全选按钮被选中
								$(".goods-check").prop('checked', true); //所有按钮都被选中
							} else {
								$(".goods-check").prop('checked', false); //else所有按钮不全选
							}
							var n1 = 0; //计算总个数
							var paymoney1 = 0; //计算总金额
							$(this).parents(".AllCheck1").prev().find(".buy_goods").find(".goodsCheck").each(function(i, val) {

								if($(val).prop("checked") == true) {
									var n = 0;
									var t = $(val).parent().next().children().find('div[class*=detail_goods_table]').children().children().find('td[class*=saleNum]').find('input[class*=saleNum1]');
									if(t.length > 0) {
										$.each(t, function(i, v) {
											n += parseInt(v.value);
										});
									}
									var m = $(val).parent().next().children().find('dl[class*=money1]').find('dd[class*=money2]').find('h6[class*=money3]').find('input[class*=money4]');

									var paymoney = m.val() * n;
									paymoney1 += paymoney;
									n1 += n;
									$(".gooodsNum").text(n1);
									$(".paymoney").text(paymoney1);
									$(".paymoney1").text(fmoney(paymoney1, 2));
								} else {
									//反选递减
									var n = 0;
									var t = $(val).parent().next().find('div[class*=dingdan_goods]').find('div[class*=detail_goods_table]').children().children().find('td[class*=saleNum]').find('input[class*=saleNum1]');
									if(t.length > 0) {
										$.each(t, function(i, v) {
											n += parseInt(v.value);
										});
									}
									var m = $(val).parent().next().children().find('dl[class*=money1]').find('dd[class*=money2]').find('h6[class*=money3]').find('input[class*=money4]');

									var paymoney = m.val() * n;
									var gooodsNum = $(".gooodsNum").text() - n;
									var p = $(".paymoney").text() - paymoney;

									$(".gooodsNum").text(gooodsNum);
									$(".paymoney").text(p);
								}

							})
							//全不选中清空合计
							if($(this).parents(".AllCheck1").prev().find(".buy_goods").find(".goodsCheck:checked").length == 0) {
								$(".gooodsNum").text('');
								$(".paymoney").text('');
								$(".paymoney1").text('');
							}

						});
					})

					//立即结算
					$(function() {

						$(".balance").click(function() {

							if($("input[name='checkedId']:checked").length == 0) {
								alert("请您至少选购一件商品");
								return false;
							}

							var arr = '[';
							$("input[name='checkedId']:checked").each(function(i, val) {

								if(i == 0) {
									arr += '{"goodsId":' + val.value + '}';
								} else {
									arr += ',{"goodsId":' + val.value + '}';
								}

							});
							arr += ']';

							if(!confirm("是否立即结算？")) {
								return;
							}
							sessionStorage.setItem("ids", arr.toString());
							location.href = "12.2.0shouhuodizhi.html";

						});

					});

					$("#pager").pager({
						pagenumber: pageNo,
						pagecount: data.data.pages,
						totalcount: data.data.total,
						buttonClickCallback: PageClick
					});

				} else if(data.code == 'OVERTIME') {
					var thisUrl = window.location.href;
					layer.alert('未登录!')
					/*layer.alert('未登录', {
						icon: 2,
						skin: 'layer-ext-myskin' 
					})*/
					//alert("未登录！");
					if(thisUrl.indexOf('1.0.0denglu.html') <= -1) {
						top.window.location.href = "1.0.0denglu.html";
					}

				} else {
					if(data.msg != '空数据') {
						alert(data.msg)
					} else {
						$('#thiscount').text(0);
					}
				}

			},
			error: function() {
				alert("error");
			}
		});
	}
	PageClick = function(pageclickednumber) {
		init(pageclickednumber);
	}
}

function goodsStockDetailById(id) {

	$("#updateMyGoodsCar").html(""); //库存

	$.ajax({
		url: urlcore + "/api/goods/updateStockById?id=" + id,
		type: "get",
		dataType: 'json',
		contentType: "application/json;charset=utf-8",
		success: function(data) {
			if(data.success == true) {
				var da = data.data;
				var money = 0;
				var num = 0; //已购多少件
				//库存
				var stockList = '';
				$.each(da.goodsStockList, function(i, n) {
					num += n.myStock==null?'':n.myStock.saleNum;
					var saleNum=n.myStock==null?'':n.myStock.saleNum;
					stockList +=
						'<li>' +
						'<span style="width: 80px;">' + n.size.sizeName + '</span>' +
						'<span style="width: 80px;">' + n.colour.colourName + '</span>' +
						'<span style="width: 80px;">' + da.tradePrice + '</span>' +
						'<span style="width: 80px;">' + n.leftNum + '</span>' +
						'<span style="width: 100px;">' +
						'<div class="lt-rt">' +
						'<input type="text" class="stockId" style="display:none;" value="' + n.id + '">' +
						'<input type="button" class="minus"  value="-">' +
						'<input type="text" class="result" value="' + saleNum + '">' +
						'<input type="button" class="add" value="+">' +
						'</div>' +
						'</span>' +
						'</li>';

				});

				money = da.tradePrice * num;

				//数量
				$(function() {

					$(".add").click(function() {
						var t = $(this).parent().find('input[class*=result]');
						t.val(parseInt(t.val()) + 1);

						//计算购买个数以及金额 加
						var num = 0;
						var money = 0;
						$('input[class*=result]').each(function(i, val) {
							num += parseInt(val.value);
						});
						$("#num").text(num);

						money = da.tradePrice * num;
						$("#money").text(fmoney(money, 2));

					})
					$(".minus").click(function() {
						var t = $(this).parent().find('input[class*=result]');
						t.val(parseInt(t.val()) - 1);
						if(parseInt(t.val()) < 0) {
							t.val(0);
						}

						//计算购买个数以及金额  减
						var money = 0;
						var num = 0;
						$('input[class*=result]').each(function(i, val) {
							num += parseInt(val.value);
						});
						$("#num").text(num);
						money = da.tradePrice * num;
						$("#money").text(fmoney(money, 2));
					})
				});

				var updateMyGoodsCar =
					'<div class="public_shuliang">' +
					'<h5>修改数量<span>关闭</span></h5>' +
					'<div class="public_shuliang_con">' +

					'<ul class="clearfix">' +
					'<li class="public_shuliang_guige">' +
					'<span  style="width: 80px;">尺码</span>' +
					'<span  style="width: 80px;">颜色</span>' +
					'<span  style="width: 80px;">单价</span>' +
					'<span  style="width: 80px;">库存</span>' +
					'<span  style="width: 100px;">进货数量</span>' +
					'</li>' +
					stockList +
					'</ul>' +
					'</div>' +
					'<div class="public_shuliang_footer">' +
					'<span>共<em id="num">' + num + '</em>件</span>' +
					'<span>小计<em id="money">' + fmoney(money, 2) + '</em>元</span>' +
					'<a href="javascript:void(0);" class="save">保存</a>' +
					'</div>' +
					'</div>';
				$('#updateMyGoodsCar').append(updateMyGoodsCar);

				//修改数量
				$(function() {
					//弹出
					$('.public-Number').on('click', function() {
						$(".public_simi_bg").fadeIn();
					});
					$(".public_shuliang h5 span,.public_shuliang_footer a").on('click', function() {
						$(".public_simi_bg").fadeOut();
					})
				});

				//12.2.0shouhuodizhi.html 立即结算
				//12.0.0gouwuche.html 加入进货车
				$(function() {
					//立即结算
					$(".save").click(function() {
						var t = $('.add').parent().find('input[class*=result]');
						var arr = '[';
						var num = 0;
						$('input[class*=result]').each(function(i, val) {
							num += parseInt(val.value);
							var stockId = $(this).parent().find('input[class*=stockId]').val();
							//if(val.value > 0) {
							if(i == 0) {
								arr += '{"id":' + stockId + ',"saleNum":' + val.value + '}';
							} else {
								arr += ',{"id":' + stockId + ',"saleNum":' + val.value + '}';
							}
							//}
						});
						arr += ']';
						if(num == 0) {
							alert("请您至少选购一件商品");
							return;
						} else {
							if(!confirm("是否修改？")) {
								return;
							}
							$.ajax({
								url: urlcore + '/api/goodsCar/add1',
								type: "post",
								contentType: "application/json;charset=utf-8",
								data: JSON.stringify({
									"myStocks": arr
								}),
								dataType: "json",
								success: function(data) {
									if(data.success == true) {
										window.location.href = '12.0.0gouwuche.html';
									}
								},
								error: function() {
									alert("error");
								}
							});

						}
					});

				});

			}
		},
		error: function() {
			alert("error");
		}
	});
}

function coll(id) {

	var cks = document.getElementsByName("checkedId");
	var arr = new Array();
	if(id != '') {
		arr.push(id);
	} else {
		for(var i = 0; i < cks.length; i++) {
			if(cks[i].checked) {
				arr.push(cks[i].value);
			}
		}
	}

	if(arr === undefined || arr.length == 0) {
		alert('至少选择一条');
		return;
	}
	if(!confirm("是否收藏此产品？")) {
		return;
	}
	$.ajax({
		url: urlcore + '/api/myCollection/add3',
		type: "post",
		contentType: "application/json;charset=utf-8",
		data: JSON.stringify({
			"goodsIds": arr.toString(),
			"status": 1
		}),
		dataType: "json",
		success: function(data) {
			if(data.success == true) {
				if(data.data == 1) {
					alert("此产品您已收藏！");
				} else {
					alert("收藏成功！");
				}

				window.location.href = '12.0.0gouwuche.html';
			}
		},
		error: function() {
			alert("error");
		}
	});
}

//批量删除
function del(id) {

	var cks = document.getElementsByName("checkedId");
	var arr = new Array();
	if(id != '') {
		arr.push(id);
	} else {
		for(var i = 0; i < cks.length; i++) {
			if(cks[i].checked) {
				arr.push(cks[i].value);
			}
		}
	}

	if(arr === undefined || arr.length == 0) {
		alert('至少选择一条');
		return;
	}
	if(!confirm("确定要删除吗？")) {
		return;
	}
	$.ajax({
		url: urlcore + "/api/goodsCar/delGoodsCarByGoodsIdOnPC?ids=" + arr.toString(),
		type: "get",
		dataType: 'json',
		contentType: "application/json;charset=utf-8",
		success: function(data) {
			if(data.success == true) {
				var da = data.data;
				if(da == true) {
					alert("删除成功");
				} else {
					alert("删除失败");
				}
				//刷新
				location.reload();
			}

		},
		error: function() {
			alert("error");
		}
	});
}
//全选/反选
function selectAll() {
	var checkboxs = document.getElementsByName("checkedId");
	for(var i = 0; i < checkboxs.length; i++) {
		var e = checkboxs[i];
		e.checked = !e.checked;
	}
}